﻿using System;
using System.Data;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using PowerToolsWeb.DataAccess;
using PowerToolsWeb.Facade;

namespace PowerToolsWeb.Admin
{
    public partial class AddProduct : AdminPageBase
    {
        public int Pid
        {
            get
            {
                string _strPid = Request.QueryString["pid"] == null ? "" : Request.QueryString["pid"].ToString();
                int pid = 0;
                int.TryParse(_strPid, out pid);
                return pid;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDDL();
                if (Pid != 0)
                {
                    BindProductInfo();
                    this.spanTitle.InnerText = "编辑商品";
                    this.btnAdd.Text = "保存";
                }
            }
        }
        void BindDDL()
        {
            Tpt_ProducttypeCollection typeList = new Tpt_ProducttypeCollection();
            typeList.ListAll();
            InitDDl(typeList.DataTable.DefaultView, ddlType, 0, 0);
        }
        //递归DDL
        private void InitDDl(DataView dv, DropDownList ddl, int parentId, int level)
        {
            ListItem ListItem1;
            dv.RowFilter = "PARANTTYPEID=" + parentId;
            foreach (DataRowView drv in dv)
            {
                string empty = "";
                for (int i = 0; i < level; i++)
                {
                    empty = empty + "　　";
                }
                ListItem1 = new ListItem();
                ListItem1.Text = empty + drv["TYPENAME"].ToString();
                ListItem1.Value = drv["TYPEID"].ToString();
                ddl.Items.Add(ListItem1);
                this.InitDDl(dv, ddl, int.Parse(ListItem1.Value), level + 1);
            }
        }
        void BindProductInfo()
        {
            Tpt_Product product = new Tpt_Product();
            if (!product.SelectByPk(Pid))
            {
                Alert(Page, "查找商品出错", "ProductList.aspx");
                return;
            }
            this.txbProductName.Text = product.PRODUCTNAME;
            this.ddlType.SelectedValue = product.TYPEID.ToString();
            this.imgPic.ImageUrl = "../" + product.PICURL;
            this.txbRemark.Text = product.REMARK;
            if (product.ISRECOMMEND)
            {
                this.rdbYes.Checked = true;
                this.rdbNo.Checked = false;
            }
            else
            {
                this.rdbYes.Checked = false;
                this.rdbNo.Checked = true;
            }
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            Response.Redirect("ProductList.aspx");
        }

        //上传
        private bool UploadPic(FileUpload _fileUpload, out string saverPath)
        {
            saverPath = "";
            if (!_fileUpload.HasFile)
            {
                Common.StaticClass.Alert(Page, "请选择文件！");
                return false;
            }
            string type = Path.GetExtension(_fileUpload.FileName).ToLower();
            if (type != ".jpg" && type != ".gif" && type != ".jpeg")
            {
                Common.StaticClass.Alert(Page, "文件类型只能是.jpg或.gif");
                return false;
            }
            long fileSize = _fileUpload.PostedFile.ContentLength;
            if (fileSize > 500 * 1024)
            {
                Common.StaticClass.Alert(Page, "文件大小不能太于500K");
                return false;
            }
            string filename = Guid.NewGuid().ToString() + type;
            //存放文件的路径
            string strSavePath = "Upload/";
            //配置文件的路径
            string strConPath = Page.Server.MapPath("/");
            try
            {
                System.IO.DirectoryInfo direc = new DirectoryInfo(strConPath + strSavePath);
                if (!direc.Exists)
                    direc.Create();
                strSavePath += filename;
                saverPath = strSavePath;
                _fileUpload.PostedFile.SaveAs(strConPath + strSavePath); //上传
                return true;
            }
            catch (Exception ex)
            {
                Common.StaticClass.Alert(Page, "上传图片出现错误：" + ex.Message);
                return false;
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (this.txbProductName.Text == "")
            {
                Alert(Page, "请输入商品名称");
                return;
            }
            if (this.ddlType.SelectedIndex < 0)
            {
                Alert(Page, "请选择类别");
                return;
            }
            if (this.txbRemark.Text.Length > 1000)
            {
                Alert(Page, "输入务备注太长");
                return;
            }
            string picPath = "";
            if (this.fuProductImg.HasFile)
                UploadPic(this.fuProductImg, out picPath);
            if (Pid == 0)
            {
                //添加
                Tpt_Product product = new Tpt_Product();
                product.PRODUCTNAME = txbProductName.Text.Trim();
                product.TYPEID = int.Parse(ddlType.SelectedValue);
                product.ISRECOMMEND = rdbYes.Checked;
                product.PICURL = picPath;
                product.REMARK = txbRemark.Text.Trim();
                if (!product.Insert())
                {
                    Alert(Page, "发布商品失败");
                    return;
                }
                Alert(Page, "发布商品成功");
            }
            else
            {
                //修改
                Tpt_Product product = new Tpt_Product();
                if (!product.SelectByPk(Pid))
                {
                    Alert(Page, "查询商品信息失败");
                    return;
                }
                product.PRODUCTNAME = txbProductName.Text.Trim();
                product.TYPEID = int.Parse(ddlType.SelectedValue);
                product.ISRECOMMEND = rdbYes.Checked;
                if (picPath != "")
                    product.PICURL = picPath;
                product.REMARK = txbRemark.Text.Trim();
                if (!product.Update())
                {
                    Alert(Page, "更新商品失败", "ProductList.aspx");
                    return;
                }
                Alert(Page, "更新商品成功", "ProductList.aspx");
            }
        }
    }
}
